onload = function (){
    daohanglan();
    selectOrder();

    document.querySelector("#search").addEventListener("keydown",keyboard)
    document.querySelector("#search").addEventListener("input",searchtips)
    document.querySelector(".searchtip").addEventListener("mouseleave",function (){
        document.querySelector(".searchtip").style.display="none"
    })

    document.querySelectorAll(".xlli").forEach(x=>{
        x.addEventListener("mouseover",xllijdbc);
    })
    a =document.querySelector(".showNavtwoson").innerHTML;
    document.querySelector(".newtuijian").addEventListener("mouseover",function (){
        document.querySelector(".showNavtwoson").innerHTML=a;
    })
    document.querySelectorAll(".gnb_nav_center").forEach(x=>{
        x.addEventListener("mouseover",function (){
            document.querySelector(".showNavtwoson").innerHTML=a;
        })
    })

    document.querySelectorAll(".xlli").forEach(x=>{
        x.addEventListener("click",function (){
            let xid = parseInt(event.target.getAttribute("id"));
            location.href="/goodsList?xid="+xid+"&show="+1;
        })
    })
}
function keyboard(){
    let keycode = event.keyCode;
    if (keycode == 13){
        search();
    }
}

function search(){
    let searchvalue = document.querySelector("#search").value;
    if(searchvalue==""){
        event.preventDefault();
    }else {
        location.href="/index?v=search&searchvalue="+searchvalue;
    }

}

function searchtips(){
    if (document.querySelector("#search").value==""){
        document.querySelector(".searchtip").style.display="none"
    }else {
        document.querySelector(".searchtip").style.display="block"
    }
    let searchvalue = document.querySelector("#search").value;
    $.ajax({
        url:"/index?v=searchtip",
        type:"post",
        data:{
            searchvalue
        },
        dataType:"json",
        success:(resp=>{
            if (resp.length>4){
                document.querySelector(".searchtip").style.height="80px";
                document.querySelector(".searchtip").style.overflowY="scroll";
            }else if (resp.length==0){
                document.querySelector(".searchtip").style.height="20px";
                document.querySelector(".searchtip").style.overflowY="";
            }else if(resp.length<4){
                document.querySelector(".searchtip").style.height="auto";
                document.querySelector(".searchtip").style.overflowY="";
            }
            let tipsbox = document.querySelector(".searchtip");
            let html=``;
            resp.forEach(r=>{
                html+=`<div onclick="bindfuture()">`+r.sname+`</div>`
            })
            tipsbox.innerHTML=html;
        })
    })
}

function bindfuture(){
    let divname = event.currentTarget.innerText;
    location.href="/index?v=search&searchvalue="+divname;
}


function xllijdbc(){
    let xid = parseInt(event.currentTarget.getAttribute("id"));
    $.ajax({
        url:"/index?v=searchli",
        type:"post",
        data:{
            xid
        },
        success:((resp)=>{
            let r = JSON.parse(resp);
            let showNavtwoson = document.querySelector(".showNavtwoson");
            let html = ``;
            r.forEach(y=>{
                html+=`<li onclick="chuandetail(${y.sid},${y.lid})">`+y.sname+`</li>`
            })
            showNavtwoson.innerHTML=html;
        })
    })
}

function chuandetail(params1,params2){
    location.href="/productDetails?sid="+params1+"&lid="+params2;
}

function Login(){
    let login = document.querySelector("#Login");
    let levitation = document.querySelector("#levitation");

    login.onmouseenter = function (){
        levitation.style.display = "block";
    }
    levitation.onmouseleave = function (){
        levitation.style.display = "none";
    }
}
function isnull(){
    location.href="/login"
}
function iscart(){
    location.href="/goods"
}
function daohanglan() {
    let allLi = document.querySelectorAll(".gnb_nav_center li");
    let show = document.querySelector("#show");
    let zzc = document.querySelector("#zzc");
    let img = document.querySelectorAll(".showNavone li span img");
    let showli = document.querySelectorAll(".showNavone li");
    let showNavtwoson = document.querySelector(".showNavtwoson");
    allLi.forEach(x => {
        x.addEventListener("mouseover", function() {
            document.querySelector("#show").style.height = 680 + "px";
            zzc.style.display = "block";

            showli.forEach(x => {
                x.style.fontWeight = "";
            })

            for (var i = 0; i < img.length; i++) {
                if (i == 0) {
                    img[i].style.display = "block";
                    document.querySelector(".showNavone li").style
                        .fontWeight = "bold";
                } else {
                    img[i].style.display = "none";
                }
            }
            allLi.forEach(y => {
                y.style.background = "#fff";
                y.style.color = "#000";
            })
            event.target.style.background = "#000";
            event.target.style.color = "#ffff";

            showNavtwoson.style.display = "block";
        })

    })
    document.querySelector(".gnb_nav").onmouseleave = function() {
        show.style.height = 0 + "px";
        zzc.style.display = "none";
        allLi.forEach(y => {
            y.style.background = "#fff";
            y.style.color = "#000";
        })
    }

    let cancel = document.querySelector("#cancelshow");
    cancel.addEventListener("click", function() {
        show.style.height = 0 + "px";
    })

    showli.forEach(x => {
        x.addEventListener("mouseover", function() {
            img.forEach(x => {
                x.style.display = "none";
            })
            let hhh = event.currentTarget.querySelector("span img");
            hhh.style.display = "block";
            showli.forEach(y => {
                y.style.fontWeight = "";
            })
            event.currentTarget.style.fontWeight = "bold";

            showNavtwoson.style.display = "none";
            setTimeout(() => {
                showNavtwoson.style.display = "block"
            }, 150);
        })
    })
}

function selectOrder(){
    $.ajax({
        url:"/payment?v=selectAll",
        type:"post",
        data:{
        },
        success:function (resp){
            console.log(resp)
        let dnumber= document.querySelector(".dnumber");
        let pay_price = document.querySelector(".pay_price");
        let com_name = document.querySelector(".com_name");
        let qr = document.querySelector(".qr");
        let html="";
        let hhtml="";
        let payp="";
        let name ="";
        console.log(resp);
            for (let i = 0; i < resp.length; i++) {
                // let tprice;
                // if (resp.length==1){
                //     tprice = (resp[i].gnumber*resp[i].xprice)
                // }else {
                //     tprice = (resp[0].gnumber*resp[0].xprice)+(resp[1].gnumber*resp[1].xprice)
                // }

                name+=`<p style="font-size: 16px;color: black;font-weight: bolder;">${resp[i].sname} ${resp[i].yname} ${resp[i].nname}</p>`
                com_name.innerHTML=name;
                html=`
                    <p>订单号：${resp[i].dnumber}</p>
                    <p>若发生退款需联系收款方协调，如付款给陌生人，请谨慎操作。交易将在<span class="pay_time" style="color: blue;font-weight: bold;">29</span>分钟后关闭，请及时付款！</p>`
            hhtml=`<p style="color: #848484;font-size: 14px;">扫一扫，付款</p>
                    <p>${resp[i].dtotal}</p>`
            payp=`<img src="img/QRcode.png">
                <p style="width: 150px;margin-left: 55px;">请使用${resp[i].pay}扫码支付</p>`
            dnumber.innerHTML=html;
            pay_price.innerHTML=hhtml;
            qr.innerHTML=payp;
        }
            time();
        }
    })
}

function time(){
    let pay_time = document.querySelector(".pay_time")
    let time=1800;
    setInterval(function (){
        time=time-1;
        let minute = parseInt(time/60);
        pay_time.innerHTML=minute;
        if (time==0){
            wait()
        }
    },1000)
}
function wait(){
    let c=2;
    $.ajax({
        url:"/payment?v=update",
        type:"post",
        data:{
            c
        },
        success:(function (){
            location.href="/MyOrdershas"
        })
    })
}
function successful(){
    let a=1;
$.ajax({
    url:"/payment?v=update",
    type:"post",
    data:{
        a
    },
    success:(function (){
        location.href="/MyOrdershas"
    })
})
}
function cancel(){
    let b=0;
    $.ajax({
        url:"/payment?v=update",
        type:"post",
        data:{
            b
        },
        success:(function (){
            location.href="/MyOrdershas"
        })
    })
}